#include<Util/datautil.h>
#include<QComboBox>
#include<QLineEdit>
#include<Util/handlefile.h>
#include<QFileInfo>
#include<QDebug>
#include<QDir>
//bool DataUtil::getTableList(QString InsertString, QList<QString> *LibsTable){
//    if(InsertString==""){
//        return false;
//    }
//    for(int index=0;index<LibsTable->count();index++){
//        if(InsertString==LibsTable->at(index)) return true;
//    }
//    LibsTable->append(InsertString);
//    return true;
// }

bool DataUtil::Remove(QTableWidget *table){
    if(table->columnCount()==1){
        QList<QString> *libsList=new QList<QString>();
        QList<QString> *SelectedLibs=new  QList<QString>();
        for(int i=0;i<table->rowCount();i++){
            libsList->append(table->item(i,0)->text());
        }
        for(int i=0;i<table->selectedItems().count();i++){
            SelectedLibs->append(table->selectedItems().at(i)->text());
        }

        this->removeListItems(SelectedLibs,libsList);
        showListTable(table,libsList);
        return true;
     }else if(table->columnCount()==2){ //两列
        QList<QList<QString>*> *libsList=new QList<QList<QString>*>();
        QList<QList<QString>*> *SelectedLibs=new  QList<QList<QString>*>();


        for(int i=0;i<table->rowCount();i++){
            QList<QString> *libL=new QList<QString>();

            libL->append(table->item(i,0)->text());
            libL->append(table->item(i,1)->text());
            libsList->append(libL);
        }
        for(int index=0;index<table->selectedItems().count();index+=2){ //加2
             QList<QString> *SelectedL=new QList<QString>();
             SelectedL->append(table->selectedItems().at(index)->text()); //该行第一列
             SelectedL->append(table->selectedItems().at(index+1)->text());//该行第二列
             SelectedLibs->append(SelectedL);
        }
        this->removeListItems(SelectedLibs,libsList); //逐行删除libsList中与SelectedLibs相同的行（只比较第一列的值）
        showListTable(table,libsList); //将修改过的libsList重新显示在列表中
        return true;
    }else{
        return false;
    }
}

bool DataUtil::removeListItems(QList<QString> *SelectedLibs,QList<QString> *AllLibsList){
    if(SelectedLibs==NULL||AllLibsList==NULL) return false;
    for(int i=0;i<SelectedLibs->count();i++){
        for(int j=0;j<AllLibsList->count();j++){
            if(SelectedLibs->at(i)==AllLibsList->at(j)){
                AllLibsList->removeAt(j);
            }
        }
    }
    return true;
}

bool DataUtil::removeListItems(QList<QList<QString> *> *SelectedLibs,QList<QList<QString> *> *AllLibsList){
    if(SelectedLibs==NULL||AllLibsList==NULL) return false;
    for(int i=0;i<SelectedLibs->count();i++){
        for(int j=0;j<AllLibsList->count();j++){
            if(SelectedLibs->at(i)->at(0)==AllLibsList->at(j)->at(0)){
                AllLibsList->removeAt(j);
            }
        }
    }
    return true;
}


bool DataUtil::AddOneRow(QTableWidget *table,QString Name,QString LineEditText){
    if(table==NULL||Name.isEmpty()||LineEditText=="") return false;
    table->insertRow(table->rowCount());
    QTableWidgetItem *newItem=new QTableWidgetItem();
    newItem->setText(Name);
    table->setItem(table->rowCount()-1,0,newItem);

    QLineEdit *lineEdit=new QLineEdit();
    lineEdit->setText(LineEditText);
    lineEdit->setStyleSheet("border:1px solid #ffffff;");
    table->setCellWidget(table->rowCount()-1,1,lineEdit);
    return true;
 }


bool DataUtil::AddOneRow(QTableWidget *table,QString Name,QList<QString> *ComboxItemList){
    if(table==NULL||Name==""||ComboxItemList==NULL) return false;
    table->insertRow(table->rowCount());
    QTableWidgetItem *newItem=new QTableWidgetItem();
    newItem->setText(Name);
    newItem->setFlags(newItem->flags() & (~Qt::ItemIsEditable));
    table->setItem(table->rowCount()-1,0,newItem);

    QComboBox *comboBox=new QComboBox();
    for(int i=0;i<ComboxItemList->count();i++){
       comboBox->addItem(ComboxItemList->at(i));
    }
    table->setCellWidget(table->rowCount()-1,1,comboBox);
    return true;
}

bool DataUtil::AddEditedRow(QTableWidget *table,QString key,QString value){
    if(table==NULL||key==""||value=="") return false;

    for(int i=0;i<table->rowCount();i++){
      if(key==table->item(i,0)->text()) return true;
    }

    table->insertRow(table->rowCount());
    QTableWidgetItem *newItem=new QTableWidgetItem();
    newItem->setText(key);
    newItem->setTextAlignment(Qt::AlignCenter);
    table->setItem(table->rowCount()-1,0,newItem);

    QTableWidgetItem *newItem1=new QTableWidgetItem();
    newItem1->setText(value);
    newItem1->setTextAlignment(Qt::AlignCenter);
    table->setItem(table->rowCount()-1,1,newItem1);
    return true;

}

bool DataUtil::showListTable(QTableWidget *table,QList<QString> *LibsList){
    //清空
    table->setRowCount(0);

   for( int i=0;i<LibsList->count();i++){
       table->insertRow(table->rowCount());
       QTableWidgetItem *newItem=new QTableWidgetItem();
       newItem->setText(LibsList->at(i));
       table->setItem(table->rowCount()-1,0,newItem);
   }
   return true;
}

bool DataUtil::showListTable(QTableWidget *table,QList<QList<QString>* >  *LibsList){
    //清空
    table->setRowCount(0);

   for( int i=0;i<LibsList->count();i++){
      this->AddEditedRow(table,LibsList->at(i)->at(0),LibsList->at(i)->at(1));
   }
   return true;
}

bool DataUtil::copySpecialFile(QTableWidget *table,QString toPath){
    if(table==NULL||toPath==NULL||toPath.isEmpty()){
        qWarning()<<"Input parameter in DataUtil::copySpecialFile can not be null."<<endl;
        return false;
    }
    QString fromPath="";
    for(int index=0;index<table->rowCount();index++){
        if(table->columnCount()==2){
            if((fromPath=table->item(index,1)->text())==""){
                qWarning()<<"the data in table in DataUtil::copySpecialFile is null."<<endl;
                return false;
            }

            if(QFileInfo(QFile(fromPath)).isFile()){
                if(!copyFileToPath(fromPath,toPath+"/"+table->item(index,0)->text(),true)) {
                    qWarning()<<"copy from "<<fromPath<<"error. in DataUtil::copySpecialFile"<<endl;
                    return false;
                }
            }else{
                if(!copyDirFiles(fromPath,toPath+"/"+table->item(index,0)->text(),true)) {
                    qWarning()<<"copy from "<<fromPath<<"error. in DataUtil::copySpecialFile"<<endl;
                    return false;
                }
            }
        }
      }

    return true;

}

bool DataUtil::AddItemToTable(QTableWidget *table, QStringList files_full){
    QString Filepath="";
   if(!files_full.isEmpty()){
       QList<QString> *LibsTable=new  QList<QString >();
       QList<QList<QString> *> *tableList=new QList<QList<QString> *>();
        //获取路径
       //获取列表内容，用于比较
       for(int i=0;i<table->rowCount();i++){

           LibsTable->append(table->item(i,0)->text());
           LibsTable->append(table->item(i,1)->text());
           tableList->append(LibsTable);
       }
       //判断是否重复，不重复则加入
       for( int i=0;i<files_full.length();i++){
            Filepath=QFileInfo(files_full[i]).absoluteFilePath();
           if(!this->AddEditedRow(table,QFileInfo(files_full[i]).fileName(),Filepath)){
               qWarning()<<"libsTable add error."<<endl;
               return false;
           }
       }
   }else{
       return false;
   }
   return true;

}

bool DataUtil::AddItemToTable(QTableWidget *table, QString Foldpath){
    QString Filepath="";
   if(Foldpath!=""){
       QList<QString> *LibsTable=new  QList<QString >();
       QList<QList<QString> *> *tableList=new QList<QList<QString> *>();
        //获取路径
       Filepath=QFileInfo(Foldpath).absoluteFilePath();

       //获取列表内容，用于比较
       for(int i=0;i<table->rowCount();i++){
           LibsTable->append(table->item(i,0)->text());
           LibsTable->append(table->item(i,1)->text());
           tableList->append(LibsTable);
       }
       //判断是否重复，不重复则加入
           if(!this->AddEditedRow(table,QFileInfo(Foldpath).fileName(),Filepath)){
               qWarning()<<"Table add error."<<endl;
               return false;
       }
   }else{
       return false;
   }
   return true;

}

//跳存
QList<QString>* DataUtil::getAllTableData(QTableWidget *table){

    if(table==NULL) return NULL;
     QList<QString> *dataList=new QList<QString>();
    for(int index=0;index<table->rowCount();index++){
        dataList->append(table->item(index,0)->text());
        dataList->append(table->item(index,1)->text());
    }
    return dataList;
}
